// test.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <opencv2\opencv.hpp>
using namespace cv;

#include<iostream>

using namespace std;

// 显示多个窗口
void window_many_imgs() {
    auto cat = imread("./picture_1663037518439.jpeg");
    auto lena = imread("./lena.jpg");

    namedWindow("Lena", WINDOW_NORMAL);

    namedWindow("Photo", WINDOW_NORMAL);

    moveWindow("Lena", 10, 10);
    moveWindow("Photo", 520, 10);

    imshow("Lena", lena);
    imshow("Photo", cat);

    resizeWindow("Lena", 512, 512);

    waitKey(0);

    destroyWindow("Lena");
    destroyWindow("Photo");


    // Create 10 windows
    for (int i = 0; i < 10; i++) {
        ostringstream ss;
        ss << "Photo " << i;
        namedWindow(ss.str());
        moveWindow(ss.str(), 20 * i, 20 * i);
        imshow(ss.str(), cat);
    }

    waitKey(0);

    destroyAllWindows();
}

void showManyImages() {

    vector<Mat> srcImage(9);
    srcImage[0] = imread("picture_1663037518439.jpeg");
    srcImage[1] = imread("picture_1663037518439.jpeg");
    srcImage[2] = imread("picture_1663037518439.jpeg");
    srcImage[3] = imread("picture_1663037518439.jpeg");
    srcImage[4] = imread("picture_1663037518439.jpeg");
    srcImage[5] = imread("picture_1663037518439.jpeg");
    srcImage[6] = imread("picture_1663037518439.jpeg");
    srcImage[7] = imread("picture_1663037518439.jpeg");
    srcImage[8] = imread("picture_1663037518439.jpeg");

    int nNumImages = srcImage.size();

    Size nSizeWindows;

    //根据图片序列数量来确定分割小窗口的形态
    switch (nNumImages) {
        case 1:
            nSizeWindows = Size(1, 1);
            break;
        case 2:
            nSizeWindows = Size(2, 1);
            break;
        case 3:
        case 4:
            nSizeWindows = Size(2, 2);
            break;
        case 5:
        case 6:
            nSizeWindows = Size(3, 2);
            break;
        case 7:
        case 8:
            nSizeWindows = Size(4, 2);
            break;
        case 9:
            nSizeWindows = Size(3, 3);
            break;
        default:
            nSizeWindows = Size(4, 3);
    }

    //设置小图像尺寸，间隙，边界
    int nShowImageSize = 200;
    int nSplitLineSize = 15;
    int nAroundLineSize = 50;
    //创建输出图像，图像大小根据输入源来确定
    const int imagesHeight =
            nShowImageSize * nSizeWindows.width + nAroundLineSize + (nSizeWindows.width - 1) * nSplitLineSize;
    const int imagesWidth =
            nShowImageSize * nSizeWindows.height + nAroundLineSize + (nSizeWindows.height - 1) * nSplitLineSize;

    cout << imagesWidth << "  " << imagesHeight << endl;

    Mat showWindowsImages(imagesWidth, imagesHeight, CV_8UC3, Scalar(0, 0, 0));

    //提取对应小图像的左上角坐标x，y
    int posX = (showWindowsImages.cols -
                (nShowImageSize * nSizeWindows.width + (nSizeWindows.width - 1) * nSplitLineSize)) / 2;
    int posY = (showWindowsImages.rows -
                (nShowImageSize * nSizeWindows.height + (nSizeWindows.height - 1) * nSplitLineSize)) / 2;
    cout << posX << "  " << posY << endl;


    int tempPosX = posX;
    int tempPosY = posY;
    //将每一幅小图像整合成一幅大图像
    for (int i = 0; i < nNumImages; i++) {
        //小图像坐标转换
        if ((i % nSizeWindows.width == 0) && (tempPosX != posX)) {
            tempPosX = posX;;
            tempPosY += (nSplitLineSize + nShowImageSize);
        }
        //利用Rect区域将小图像置于大图像的相应区域
        // Rect 矩形等比例缩小
        Mat tempImage = showWindowsImages(Rect(tempPosX, tempPosY, nShowImageSize, nShowImageSize));

        resize(srcImage[i], tempImage, Size(nShowImageSize, nShowImageSize));//利用resize函数实现图像缩放

        tempPosX += (nSplitLineSize + nShowImageSize);
    }


    imshow("单窗口显示多图片", showWindowsImages);

    waitKey(0);
    system("pause");
}

int main() {
    //图像源输入

    window_many_imgs();

    showManyImages();

    return 0;
}